package com.ruoyi.project.labappointment.service;

import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.lab.domain.LabAppointmentDto;
import com.ruoyi.project.labappointment.domain.AddListParam;
import com.ruoyi.project.labappointment.domain.AddList;
import com.ruoyi.project.labappointment.domain.ApplyParam;
import com.ruoyi.project.labappointment.mapper.LabAppointmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class LabAppointmentServiceImpl implements LabAppointmentService{

    @Autowired
    private LabAppointmentMapper labAppointmentMapper;


    @Override
    public List<AddList> selectAddList(AddListParam addListParam) {
        if(addListParam.getExperimentDate()==null || "".equals(addListParam.getExperimentDate())){
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            addListParam.setExperimentDate(format.format(new Date()).substring(0,10));
        }
        return labAppointmentMapper.selectAddList(addListParam);
    }

    @Override
    public AjaxResult applyLab(List<ApplyParam> applyParam,Long userId) {
        //System.err.println(applyParam);
        //日期转换
        SimpleDateFormat dft= new SimpleDateFormat("yyyy-MM-dd");
        List<String>ids=new ArrayList<>();
        for(ApplyParam applyParams:applyParam){
            if(dft.format(new Date()).toString().compareTo(applyParams.getApplyTime())>0){
                return new AjaxResult(AjaxResult.Type.ERROR,"不可预约小于当前日期的实验室！");
            }
            Long generatedKeys=labAppointmentMapper.selectLabTime(applyParams);
            if(generatedKeys==0){
                return new AjaxResult(AjaxResult.Type.ERROR,applyParams.getApplyTime()+"第:"+applyParams.getSection()+"已被预约!请更换！");
            }else{
                ids.add(generatedKeys.toString());
            }
        }
        //修改实验室节次状态
        for(ApplyParam applyParams:applyParam){
            labAppointmentMapper.updateLabDate(applyParams);
        }
        //添加到用户实验室预约表
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("userId", userId);
        //remark即备注
        map.put("remark",applyParam.get(0).getRemark());
        labAppointmentMapper.addLabAppointment(map);
        return AjaxResult.success(AjaxResult.Type.SUCCESS,"申请成功！");
    }

    @Override
    public List<LabAppointmentDto> selectAppointemntSelect(AddListParam addListParam, Long userId) {
        return labAppointmentMapper.selectAppointemntSelect(addListParam,userId);
    }

    @Override
    @Transactional
    public AjaxResult quitApply(Long id) {
//        int a=labAppointmentMapper.quitApply(id);
        Long labDateId=labAppointmentMapper.selectlabDateIdById(id);
        //修改实验室预约状态
        labAppointmentMapper.updateLabDateStatus(labDateId);
        //删除预约信息
        int delete=labAppointmentMapper.quitApply(id);
        if(delete>0){
            return new AjaxResult(AjaxResult.Type.SUCCESS,"撤销成功");
        }else{
            return new AjaxResult(AjaxResult.Type.ERROR,"撤销失败");
        }
    }


    @Override
    public Boolean goOut(Long id) {
        return labAppointmentMapper.goOut(id);
    }

    @Override
    public Boolean refuse(Long id) {
        return labAppointmentMapper.refuse(id);
    }

    /**
     * 删除实验室预约
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public boolean deleteAppoinment(String ids) {
        labAppointmentMapper.updateAppointmentStatus(Convert.toStrArray(ids));
        labAppointmentMapper.deleteAppointment(Convert.toStrArray(ids));
        return true;
    }

   /* @Transactional
    public boolean deleteAppoinment2(String[] ids) {
        System.err.println(Arrays.toString(ids));
        labAppointmentMapper.updateAppointmentStatus(ids);
        labAppointmentMapper.deleteAppointment(ids);
        return true;
    }*/

    /**
     * 修改保存      先删除已有的，后重新添加
     * @param applyParam
     * @param userId
     * @return
     */
    /*@Override
    public AjaxResult applyLab2(List<ApplyParam> applyParam, Long userId) {
        //System.err.println(applyParam);
        String id=applyParam.get(0).getId().toString();
        //System.err.println(id);
        //调用删除的方法
        LabAppointmentServiceImpl labAppointmentService = new LabAppointmentServiceImpl();
        labAppointmentService.deleteAppoinment(id);
        //System.err.println(applyParam);
        //日期转换
        SimpleDateFormat dft= new SimpleDateFormat("yyyy-MM-dd");
        List<String>ids=new ArrayList<>();
        for(ApplyParam applyParams:applyParam){
            if(dft.format(new Date()).toString().compareTo(applyParams.getApplyTime())>0){
                return new AjaxResult(AjaxResult.Type.ERROR,"不可预约小于当前日期的实验室！");
            }
            Long generatedKeys=labAppointmentMapper.selectLabTime(applyParams);
            if(generatedKeys==0){
                return new AjaxResult(AjaxResult.Type.ERROR,applyParams.getApplyTime()+"第:"+applyParams.getSection()+"已被预约!请更换！");
            }else{
                ids.add(generatedKeys.toString());
            }
        }
        //修改实验室节次状态
        for(ApplyParam applyParams:applyParam){
            labAppointmentMapper.updateLabDate(applyParams);
        }
        //添加到用户实验室预约表
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("userId", userId);

        //remark即备注
        map.put("remark",applyParam.get(0).getRemark());
        labAppointmentMapper.addLabAppointment(map);
        return AjaxResult.success(AjaxResult.Type.SUCCESS,"申请成功！");
    }*/
}



















